package cn.hollis.nft.turbo.test.service;

import cn.hollis.nft.turbo.test.entity.User;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserService implements InitializingBean {



    /**
     * 通过用户ID查询用户信息
     *
     * @param userId
     * @return
     */
    @Cached(name = ":users:cache:id:", expire = 3000, cacheType = CacheType.BOTH, key = "#userId", cacheNullValue = true)
    @CacheRefresh(refresh = 60, timeUnit = TimeUnit.MINUTES)
    public User findById(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setUsername("fangbin");
        user.setPassword("123456");
        log.info("init data to redis......");
        return null;
    }

  
    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @CacheInvalidate(name = ":users:cache:id:", key = "#user.id")
    public Boolean modify(User user) {
        log.info("init data to redis......,CacheInvalidate");
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}